﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using attendance.L3Bussobject;
using attendance.L2Busslogic;
using System.Net;

namespace attendance.L4View.student
{
    public partial class student_login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('名字不能为空 ');</script>");
                return;

            }
            if (TextBox2.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('学号不能为空 ');</script>");
                return;

            }
            studentOB mydd = new studentOB();
            mydd = studentBL.Getwordsingle(TextBox1.Text.Trim());
            string IP = GetLocalIp();
            qiandaoOB mydd1 = new qiandaoOB();
            if (mydd.name.Trim() == "访问数据库不成功！")
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('没有该学生 ');</script>");
                return;
            }
            if (mydd.name == TextBox1.Text && mydd.numb == TextBox2.Text && mydd.photo!=" ")
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('登录成功 ');</script>");
                mydd1.q_name = TextBox1.Text;
                mydd1.q_numb = TextBox2.Text;
                mydd1.q_date = DateTime.Now.ToString();
                mydd1.q_ip = IP;
                mydd1.q_photo = "";
                mydd1.photo = mydd.photo;
                Session["login"] = TextBox1.Text;
                if (qiandaoBL.Addfill(mydd1))
                {
                    Response.Redirect("~/L4View/student/student_qiandao.aspx");
                }
                return;
            }
            if (mydd.name == TextBox1.Text && mydd.numb == TextBox2.Text && mydd.photo == "")
            {
                Session["add"] = TextBox1.Text;
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('信息不完整，单击“确定”跳转到拍照页面，补全信息 ');</script>");
                Response.Redirect("~/L4View/student/student_.aspx");
                return;
            }
            if ((mydd.name == TextBox1.Text && mydd.numb != TextBox2.Text) || (mydd.name != TextBox1.Text && mydd.numb == TextBox2.Text))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('用户名或密码错误 ');</script>");
                TextBox1.Text = " ";
                TextBox2.Text = " ";
                return;
            }
        }

        static string GetLocalIp()
        {
            string hostname = Dns.GetHostName();
            IPHostEntry localhost = Dns.GetHostByName(hostname);
            IPAddress localaddr = localhost.AddressList[0];
            return localaddr.ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/L4View/student/student_register.aspx");
        }
    }
}